package session1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Main {
	public static void main(String[] args) {
		OverallFrame frame=new OverallFrame();
		frame.setTitle("大学生个人信息调查问卷");
		frame.setSize(960,1024);
		frame.setResizable(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
class OverallFrame extends JFrame{
//	enum TITLES{NAME,STU_NUM,AGE,MAJOR,HOBBY,SUGGESTIONS}
	final String[] titles=
			{"姓名","学号","年龄","学院","专业","所选课程","个人兴趣爱好","对学校的建议"},
	exitChoices={"提交","重置","退出"},
	schools={"光电学院","机械学院","材料学院","外语学院","理学院"},
	courses={"高等数学","Java编程","数据结构","大学物理",},
	hobbies={"音乐","体育","旅游","阅读",},
	majors={"计算机科学与技术","大数据科学与技术",
			"光电信息科学与技术","新闻学","金融学","汽车工程","英语"},
		genders ={"男","女"};
	JPanel mainPanel;
	GridBagConstraints gridBagConstraints;
	LayoutManager layout;
	final Font font=new Font("Consolas", Font.PLAIN,0x40);

	public OverallFrame() throws HeadlessException {
		int location=0;
		this.gridBagConstraints = new GridBagConstraints();
		this.gridBagConstraints.insets=
				new Insets(3,5,3,10);
		this.mainPanel=new JPanel();
		this.mainPanel.setLayout(layout=new GridBagLayout());

		var textField=new JTextField(10);
		textField.setFont(font);
		addElement(location,textField);
		location++;

		textField=new JTextField(10);
		textField.setFont(font);
		addElement(location,textField);
		location++;

		addElement(location,myPanel(genders,false));
		location++;
		addElement(location++,new JComboBox<>(schools));
		addElement(location++,new JList<>(majors));

		addElement(location,myPanel(courses,true));
		location++;
		addElement(location,myPanel(hobbies,true));
		location++;
		addElement(location,new JScrollPane(new JTextArea(8,20)));
		location++;

		System.out.println(gridBagConstraints.gridy=location);
		gridBagConstraints.gridx=0;
		for(String exitChoice:exitChoices){
			JButton choice=new JButton((exitChoice));
			choice.addMouseListener(new ExitMouseListener(exitChoice,choice));
			mainPanel.add(choice,gridBagConstraints);
			gridBagConstraints.gridx++;
		}
		setContentPane(mainPanel);
		pack();
		setVisible(true);
	}
	public void addElement(int location,Component component){
		addElement(location,titles[location],component);
	}
	public void addElement(int location,String labelName, Component component){
		gridBagConstraints.gridy=location;

		gridBagConstraints.gridx=0;
		JLabel label=new JLabel(labelName);
		label.setFont(font);
		mainPanel.add(label,gridBagConstraints);
		gridBagConstraints.gridx=1;
		mainPanel.add(component,gridBagConstraints);
	}
	public Panel myPanel(String[] choices, boolean choose_multiple){
		Panel ret=new Panel();
		if(choose_multiple)
			ret.setLayout(new GridLayout(choices.length,1));
		for(String choice:choices){
			if(choose_multiple)
				ret.add(new JCheckBox(choice));
			else ret.add(new JRadioButton(choice));
		}
		return ret;
	}
}
class ExitMouseListener implements MouseListener{
	String finishChoice;
	Component actComponent;
	public ExitMouseListener(String finishChoice, Component actComponent) {
		this.finishChoice = finishChoice;
		this.actComponent = actComponent;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		JOptionPane.showConfirmDialog(actComponent,finishChoice);
	}
	@Override
	public void mousePressed(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
}